package com.javainterface.service;

import com.alibaba.fastjson.JSON;

import com.banboocloud.Codec.BamboocloudFacade;
import com.javainterface.entity.AttributeEntity;
import com.javainterface.entity.OrgEntity;
import com.javainterface.entity.UserEntity;
import com.javainterface.mapper.JavaInterfaceMapper;
import com.javainterface.utils.BambooCloudMethodUtils;
import com.javainterface.utils.BamboocloudUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * 竹云对接用户接口
 */
@Service
public class BambookService {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    BamboocloudUtils bamboocloudUtils;

    @Autowired
    BambooCloudMethodUtils bambooCloudMethodUtils;

    @Autowired
    JavaInterfaceMapper javaInterfaceMapper;

    //清除数据库缓存
    public void clearMySqlBuffer() {
        String casUrl = "http://10.0.12.66:5080/cosmo-mmp/v1/clearcaches/clearCacheByName?serviceName=cosmo-cas&selfTenantIdForSuspendButton=platform&optionTenantId=platform";
        String mmpUrl = "http://10.0.12.72:5080/cosmo-mmp/v1/clearcaches/clearCacheByName?serviceName=cosmo-mmp&selfTenantIdForSuspendButton=platform&optionTenantId=platform";
        try {
            restTemplate.getForObject(casUrl, String.class);
            restTemplate.getForObject(mmpUrl, String.class);
            System.out.println("数据库缓存清除成功...");
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    //映射BIM系统属性
    public String schemaService(String bodyparam) {
        System.out.println("SchemaService--param-->" + bodyparam);
        //报文解密
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");

        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);
        //验证签名
//        if (BamboocloudUtils.verify(reqmap, "MD5").booleanValue()) {
        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        Map<String, Object> schema = new HashMap<String, Object>();
        List<AttributeEntity> attrList = new ArrayList<AttributeEntity>();

        //设置用户表映射
        AttributeEntity attr1 = new AttributeEntity();
        attr1.setType("String");
        attr1.setName("display_name");
        attr1.setRequired(true);
        attr1.setMultivalued(false);
        attrList.add(attr1);

        AttributeEntity attr2 = new AttributeEntity();
        attr2.setType("String");
        attr2.setName("login_uid");
        attr2.setRequired(true);
        attr2.setMultivalued(false);
        attrList.add(attr2);

        AttributeEntity attr3 = new AttributeEntity();
        attr3.setType("String");
        attr3.setName("login_pwd");
        attr3.setRequired(true);
        attr3.setMultivalued(false);
        attrList.add(attr3);

        AttributeEntity attr4 = new AttributeEntity();
        attr4.setType("int");
        attr4.setName("active_flag");
        attr4.setRequired(true);
        attr4.setMultivalued(false);
        attrList.add(attr4);

        AttributeEntity attr5 = new AttributeEntity();
        attr5.setType("int");
        attr5.setName("is_system");
        attr5.setRequired(true);
        attr5.setMultivalued(false);
        attrList.add(attr5);

        AttributeEntity attr6 = new AttributeEntity();
        attr6.setType("int");
        attr6.setName("creator");
        attr6.setRequired(true);
        attr6.setMultivalued(false);
        attrList.add(attr6);

        AttributeEntity attr7 = new AttributeEntity();
        attr7.setType("String");
        attr7.setName("creat_date");
        attr7.setRequired(true);
        attr7.setMultivalued(false);
        attrList.add(attr7);

        AttributeEntity attr8 = new AttributeEntity();
        attr8.setType("int");
        attr8.setName("last_update_by");
        attr8.setRequired(true);
        attr8.setMultivalued(false);
        attrList.add(attr8);

        AttributeEntity attr9 = new AttributeEntity();
        attr9.setType("String");
        attr9.setName("last_update_date");
        attr9.setRequired(true);
        attr9.setMultivalued(false);
        attrList.add(attr9);

        AttributeEntity attr10 = new AttributeEntity();
        attr10.setType("String");
        attr10.setName("memo");
        attr10.setRequired(true);
        attr10.setMultivalued(false);
        attrList.add(attr10);

        AttributeEntity attr11 = new AttributeEntity();
        attr11.setType("int");
        attr11.setName("change_login_pwd_flag");
        attr11.setRequired(true);
        attr11.setMultivalued(false);
        attrList.add(attr11);

        AttributeEntity attr12 = new AttributeEntity();
        attr12.setType("int");
        attr12.setName("validity");
        attr12.setRequired(true);
        attr12.setMultivalued(false);
        attrList.add(attr12);

        AttributeEntity attr13 = new AttributeEntity();
        attr13.setType("int");
        attr13.setName("u_type");
        attr13.setRequired(true);
        attr13.setMultivalued(false);
        attrList.add(attr13);

        AttributeEntity attr14 = new AttributeEntity();
        attr14.setType("int");
        attr14.setName("p_id");
        attr14.setRequired(true);
        attr14.setMultivalued(false);
        attrList.add(attr14);

        AttributeEntity attr15 = new AttributeEntity();
        attr15.setType("String");
        attr15.setName("org_id");
        attr15.setRequired(true);
        attr15.setMultivalued(false);
        attrList.add(attr15);

        AttributeEntity attr16 = new AttributeEntity();
        attr16.setType("String");
        attr16.setName("u_code");
        attr16.setRequired(true);
        attr16.setMultivalued(false);
        attrList.add(attr16);

        AttributeEntity attr17 = new AttributeEntity();
        attr17.setType("String");
        attr17.setName("order_num");
        attr17.setRequired(true);
        attr17.setMultivalued(false);
        attrList.add(attr17);

        AttributeEntity attr18 = new AttributeEntity();
        attr18.setType("String");
        attr18.setName("three");
        attr18.setRequired(true);
        attr18.setMultivalued(false);
        attrList.add(attr18);

        AttributeEntity attr19 = new AttributeEntity();
        attr19.setType("String");
        attr19.setName("expire_date");
        attr19.setRequired(true);
        attr19.setMultivalued(false);
        attrList.add(attr19);


        //设置组织表映射
        List<AttributeEntity> orgattrlist = new ArrayList<AttributeEntity>();
        AttributeEntity org1 = new AttributeEntity();
        org1.setType("String");
        org1.setName("name");
        org1.setRequired(true);
        org1.setMultivalued(false);
        orgattrlist.add(org1);

        AttributeEntity org2 = new AttributeEntity();
        org2.setType("String");
        org2.setName("short_name");
        org2.setRequired(true);
        org2.setMultivalued(false);
        orgattrlist.add(org2);

        AttributeEntity org3 = new AttributeEntity();
        org3.setType("String");
        org3.setName("level_num");
        org3.setRequired(true);
        org3.setMultivalued(false);
        orgattrlist.add(org3);

        AttributeEntity org4 = new AttributeEntity();
        org4.setType("String");
        org4.setName("p_id");
        org4.setRequired(true);
        org4.setMultivalued(false);
        orgattrlist.add(org4);

        AttributeEntity org5 = new AttributeEntity();
        org5.setType("int");
        org5.setName("order_num");
        org5.setRequired(true);
        org5.setMultivalued(false);
        orgattrlist.add(org5);

        AttributeEntity org6 = new AttributeEntity();
        org6.setType("String");
        org6.setName("memo");
        org6.setRequired(true);
        org6.setMultivalued(false);
        orgattrlist.add(org6);

        AttributeEntity org7 = new AttributeEntity();
        org7.setType("String");
        org7.setName("type");
        org7.setRequired(true);
        org7.setMultivalued(false);
        orgattrlist.add(org7);

        AttributeEntity org8 = new AttributeEntity();
        org8.setType("String");
        org8.setName("org_code");
        org8.setRequired(true);
        org8.setMultivalued(false);
        orgattrlist.add(org8);

        AttributeEntity org9 = new AttributeEntity();
        org9.setType("String");
        org9.setName("ext1");
        org9.setRequired(true);
        org9.setMultivalued(false);
        orgattrlist.add(org9);

        AttributeEntity org10 = new AttributeEntity();
        org10.setType("String");
        org10.setName("ext2");
        org10.setRequired(true);
        org10.setMultivalued(false);
        orgattrlist.add(org10);

        AttributeEntity org105 = new AttributeEntity();
        org105.setType("int");
        org105.setName("org_type");
        org105.setRequired(true);
        org105.setMultivalued(false);
        orgattrlist.add(org105);


        AttributeEntity org11 = new AttributeEntity();
        org11.setType("int");
        org11.setName("creator");
        org11.setRequired(true);
        org11.setMultivalued(false);
        orgattrlist.add(org11);

        AttributeEntity org12 = new AttributeEntity();
        org12.setType("String");
        org12.setName("creat_date");
        org12.setRequired(true);
        org12.setMultivalued(false);
        orgattrlist.add(org12);

        AttributeEntity org13 = new AttributeEntity();
        org13.setType("int");
        org13.setName("last_update_by");
        org13.setRequired(true);
        org13.setMultivalued(false);
        orgattrlist.add(org13);

        AttributeEntity org14 = new AttributeEntity();
        org14.setType("String");
        org14.setName("last_update_date");
        org14.setRequired(true);
        org14.setMultivalued(false);
        orgattrlist.add(org14);

        //将组织列表封装成map格式的列表
        List<Map> orgList = new ArrayList<>();
        for (int i = 0; i < orgattrlist.size(); i++) {
            Map<String, Object> orgMap = new HashMap<>();
            orgMap.put("multivalued", orgattrlist.get(i).isMultivalued());
            orgMap.put("name", orgattrlist.get(i).getName());
            orgMap.put("type", orgattrlist.get(i).getType());
            orgMap.put("required", orgattrlist.get(i).isRequired());
            orgList.add(orgMap);
        }

        //将用户列表封装成map格式的列表
        List<Map> userList = new ArrayList<>();
        for (int i = 0; i < attrList.size(); i++) {
            Map<String, Object> userMap = new HashMap<>();
            userMap.put("multivalued", attrList.get(i).isMultivalued());
            userMap.put("name", attrList.get(i).getName());
            userMap.put("type", attrList.get(i).getType());
            userMap.put("required", attrList.get(i).isRequired());
            userList.add(userMap);
        }

//            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(makeMap.makeSchemaMap());
//            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(String.valueOf(tableMap));
//            List<AttributeEntity> orgs = jsonObject.getObject("organization", List.class);
//            System.out.println(orgs.toString());
//
//            List<AttributeEntity> accs = jsonObject.getObject("account", List.class);
//            System.out.println(accs.toString());
//            schema.put("account", accs);
//            schema.put("organization", orgs);

        //将列表放进map中
        schema.put("account", userList);
        schema.put("organization", orgList);
        schema.put("bimRequestId", reqmap.get("bimRequestId"));

        String mapJson = JSON.toJSONString(schema);
        System.out.println("response---json-->" + mapJson);

        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
//        }
//        return null;
    }

    //创建用户
    public String userCreateService(String bodyparam) {
        System.out.println("UserCreateService--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);
        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        String display_name = (String) reqmap.get("display_name");
        String login_uid = (String) reqmap.get("login_uid");
//        String login_pwd = (String) reqmap.get("login_pwd");
        String login_pwd = "EA3C7C91D2FE7AF78026";
//        String active_flag = (String) reqmap.get("active_flag");
        String active_flag = "1";
//        String is_system = (String) reqmap.get("is_system");
        String is_system = "0";
        if (login_uid.equals("admin")) {
            is_system = "1";
        }
//        String creator = (String) reqmap.get("creator");
        String creator = "3";
        String creat_date = (String) reqmap.get("creat_date");
        //截取create_date到毫秒之前
        creat_date = creat_date.split(".")[0];
//        String last_update_by = (String) reqmap.get("last_update_by");
        String last_update_by = "3";
        String last_update_date = (String) reqmap.get("last_update_date");
        String memo = (String) reqmap.get("memo");
//        String change_login_pwd_flag = (String) reqmap.get("change_login_pwd_flag");
        String change_login_pwd_flag = "0";
//        String validity = (String) reqmap.get("validity");
        String validity = "-1";
//        String u_type = (String) reqmap.get("u_type");
        String u_type = "0";
        if (login_uid.equals("admin")) {
            u_type = "3";
        }
//        String p_id = (String) reqmap.get("p_id");
        String p_id = "3";
        String org_id = (String) reqmap.get("org_id");
//        String u_code = (String) reqmap.get("u_code");
        String u_code = "";
        String order_num = (String) reqmap.get("order_num");
        String three = (String) reqmap.get("three");
//        String expire_date = (String) reqmap.get("expire_date");
        String expire_date = "-1";

        try {
            //查询用户id
            int userid = javaInterfaceMapper.selectUserIdByLoginuserid(login_uid).getUserid();
            if (userid > 0) {
                Map<String, Object> schema = new HashMap<String, Object>();
                schema.put("uid", "null");
                schema.put("bimRequestId", reqmap.get("bimRequestId"));
                schema.put("resultCode", "500");
                schema.put("message", "插入数据重复");
                String mapJson = JSON.toJSONString(schema);
                System.out.println("response----->" + mapJson);
                mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
                return mapJson;
            }
        } catch (Exception e) {
            System.out.println(e);
        }


        // 插入数据到数据库表
        javaInterfaceMapper.userInsert(
                display_name, login_uid, login_pwd, active_flag, is_system, creator,
                creat_date, last_update_by, last_update_date, memo, change_login_pwd_flag,
                validity, u_type, p_id, org_id, u_code, order_num, three, expire_date
        );


        try {
            //查询用户id
            int userid = javaInterfaceMapper.selectUserIdByLoginuserid(login_uid).getUserid();
            javaInterfaceMapper.userDetailInsert(userid + "", "1", "1", "1", "1");
            javaInterfaceMapper.orgUserMapInsert(userid + "", org_id);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", userid + "");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        } catch (Exception e) {
            System.out.println(e);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", "null");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "500");
            schema.put("message", "error...");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }

    }

    //修改用户
    public String userUpdateService(String bodyparam) {
        System.out.println("userUpdateService--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);
        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        String userId = (String) reqmap.get("bimUid");
        String display_name = (String) reqmap.get("display_name");
        String login_uid = (String) reqmap.get("login_uid");
        String login_pwd = (String) reqmap.get("login_pwd");
        String creat_date = (String) reqmap.get("creat_date");
        String last_update_date = (String) reqmap.get("last_update_date");
        String memo = (String) reqmap.get("memo");
        String org_id = (String) reqmap.get("org_id");
        String order_num = (String) reqmap.get("order_num");
        String three = (String) reqmap.get("three");

        String expire_date = "-1";
        String active_flag = "1";
        String is_system = "0";
        String creator = "3";
        String validity = "-1";
        String u_type = "0";
        String p_id = "3";
        String change_login_pwd_flag = "0";
        String u_code = "0";
        String last_update_by = "3";

        try {
            if (javaInterfaceMapper.selectUserByUserId(userId) == null) {
                Map<String, Object> schema = new HashMap<String, Object>();
                schema.put("uid", userId);
                schema.put("bimRequestId", reqmap.get("bimRequestId"));
                schema.put("resultCode", "500");
                schema.put("message", "没有此用户");
                String mapJson = JSON.toJSONString(schema);
                System.out.println("response----->" + mapJson);
                mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
                return mapJson;
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        try {
            javaInterfaceMapper.updateUserByUserId(
                    userId, display_name, login_uid, login_pwd, active_flag,
                    is_system, creator, creat_date, last_update_by, last_update_date,
                    memo, change_login_pwd_flag, validity, u_type, p_id, org_id,
                    u_code, order_num, three, expire_date);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", userId + "");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "更新成功");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        } catch (Exception e) {
            System.out.println(e);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", userId);
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "500");
            schema.put("message", "更新失败");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }


    }

    //删除用户(逻辑)
    public String userDeleteService(String bodyparam) {
        System.out.println("userDeleteService--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);
        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        String userId = (String) reqmap.get("bimUid");

        try {
            if (javaInterfaceMapper.selectUserByUserId(userId) == null) {
                Map<String, Object> schema = new HashMap<String, Object>();
                schema.put("uid", userId);
                schema.put("bimRequestId", reqmap.get("bimRequestId"));
                schema.put("resultCode", "500");
                schema.put("message", "无此用户");
                String mapJson = JSON.toJSONString(schema);
                System.out.println("response----->" + mapJson);
                mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
                return mapJson;
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        try {
            javaInterfaceMapper.deleteUserByUserId(userId);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", userId);
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "删除成功");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        } catch (Exception e) {
            System.out.println(e);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", userId);
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "500");
            schema.put("message", "删除用户失败");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }
    }

    //查询所有用户
    public String QueryAllUserIdsService(String bodyparam) {
        System.out.println("userDeleteService--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);
        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        try {
            List<UserEntity> userEntityList = javaInterfaceMapper.QueryAllUsers();
            List<String> userIdList = new ArrayList<>();
            for (int i = 0; i < userEntityList.size(); i++) {
                userIdList.add(userEntityList.get(i).getUserid() + "");
            }
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("resultCode", "0");
            schema.put("message", "QueryAllUserIdsService success...");
            schema.put("userIdList", userIdList);
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;

        } catch (Exception e) {
            System.out.println(e);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("resultCode", "500");
            schema.put("message", "QueryAllUserIdsService error...");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }


    }

    //根据用户id查询用户
    public String QueryUserByIdService(String bodyparam) {
        System.out.println("selectOrgList--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);

        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        String userId = (String) reqmap.get("bimUid");

        try {
            UserEntity userEntity = javaInterfaceMapper.selectUserByUserId(userId);
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("display_name", userEntity.getDisplay_name());
            userInfo.put("login_uid", userEntity.getLogin_uid());
            userInfo.put("login_pwd", userEntity.getLogin_pwd());
            userInfo.put("active_flag", userEntity.getActive_flag());
            userInfo.put("is_system", userEntity.getIs_system());
            userInfo.put("creator", userEntity.getCreator());
            userInfo.put("creat_date", userEntity.getCreat_date());
            userInfo.put("last_update_by", userEntity.getLast_update_by());
            userInfo.put("last_update_date", userEntity.getLast_update_date());
            userInfo.put("memo", userEntity.getMemo());
            userInfo.put("change_login_pwd_flag", userEntity.getChange_login_pwd_flag());
            userInfo.put("validity", userEntity.getValidity());
            userInfo.put("u_type", userEntity.getU_type());
            userInfo.put("p_id", userEntity.getP_id());
            userInfo.put("org_id", userEntity.getOrg_id());
            userInfo.put("u_code", userEntity.getU_code());
            userInfo.put("order_num", userEntity.getOrder_num());
            userInfo.put("three", userEntity.getThree());
            userInfo.put("expire_date", userEntity.getExpire_date());

            Map<String, Object> schema = new HashMap<String, Object>();

            schema.put("account", userInfo);
            schema.put("resultCode", "0");
            schema.put("message", "success...");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        } catch (Exception e) {
            System.out.println(e);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("resultCode", "500");
            schema.put("message", "error...");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }
    }


    //创建组织
    public String orgCreateService(String bodyparam) {
        System.out.println("OrgCreateService--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);

        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        String name = (String) reqmap.get("name");
        String short_name = (String) reqmap.get("short_name");
        String org_code = (String) reqmap.get("org_code");
        String level_num = (org_code.length() / 3) + "";
//        String level_num = (String) reqmap.get("level_num");
        String p_id = (String) reqmap.get("p_id");
        int order_num = 0;
        try {
            order_num = (int) reqmap.get("order_num");
        } catch (Exception e) {
            order_num = 0;
        }
        String memo = (String) reqmap.get("memo");
        String type = (String) reqmap.get("type");

        String ext1 = (String) reqmap.get("__NAME__");
        String ext2 = (String) reqmap.get("ext2");
        String org_type = (String) reqmap.get("org_type");
        int creator = 0;
        try {
            creator = (int) reqmap.get("creator");
        } catch (Exception e) {
            creator = 0;
        }
        String creat_date = "2021-12-21 22:09:59";
        int last_update_by = 0;
        try {
            last_update_by = (int) reqmap.get("last_update_by");
        } catch (Exception e) {
            last_update_by = 0;
        }
        String last_update_date = (String) reqmap.get("last_update_date");

        try {
            //查询主键
            int orgid = javaInterfaceMapper.selectOrgIdByExt1(ext1).getOrg_id();
            if (orgid > 0) {
                Map<String, Object> schema = new HashMap<String, Object>();
                schema.put("uid", "null");
                schema.put("bimRequestId", reqmap.get("bimRequestId"));
                schema.put("resultCode", "500");
                schema.put("message", "插入__NAME__重复");
                String mapJson = JSON.toJSONString(schema);
                System.out.println("response----->" + mapJson);
                mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
                return mapJson;
            }
        } catch (Exception e) {
            System.out.println(e);
        }


        //插入数据到数据库
        javaInterfaceMapper.orgInsert(
                name, short_name, level_num, p_id, order_num, memo,
                type, org_code, ext1, ext2, org_type, creator, creat_date,
                last_update_by, last_update_date
        );

        bamboocloudUtils.checkUsernamePassword(username, password);

        try {
            //查询主键
            int orgid = javaInterfaceMapper.selectOrgIdByExt1(ext1).getOrg_id();
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", orgid + "");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "success");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        } catch (Exception e) {
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", "null");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "500");
            schema.put("message", "插入__NAME__重复");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }

    }

    //修改组织
    public String orgUpdateService(String bodyparam) {
        System.out.println("orgUpdateService--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);

        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        String orgId = (String) reqmap.get("bimOrgId");
        String name = (String) reqmap.get("name");
        String short_name = (String) reqmap.get("short_name");
        String level_num = (String) reqmap.get("level_num");
        String p_id = (String) reqmap.get("p_id");
        int order_num = 0;
        try {
            order_num = (int) reqmap.get("order_num");
        } catch (Exception e) {
            order_num = 0;
        }
        String memo = (String) reqmap.get("memo");
        String type = (String) reqmap.get("type");
        String org_code = (String) reqmap.get("org_code");
        String ext1 = (String) reqmap.get("__NAME__");
        String ext2 = (String) reqmap.get("ext2");
        String org_type = (String) reqmap.get("org_type");
        int creator = 0;
        try {
            creator = (int) reqmap.get("creator");
        } catch (Exception e) {
            creator = 0;
        }
        String creat_date = "2021-12-21 22:09:59";
        int last_update_by = 0;
        try {
            last_update_by = (int) reqmap.get("last_update_by");
        } catch (Exception e) {
            last_update_by = 0;
        }
        try {
            if (javaInterfaceMapper.selectOrgByOrgId(orgId) == null) {
                Map<String, Object> schema = new HashMap<String, Object>();
                schema.put("uid", "null");
                schema.put("bimRequestId", reqmap.get("bimRequestId"));
                schema.put("resultCode", "500");
                schema.put("message", "此组织不存在");
                String mapJson = JSON.toJSONString(schema);
                System.out.println("response----->" + mapJson);
                mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
                return mapJson;
            }

        } catch (Exception e) {
            System.out.println(e);
        }

        try {
            javaInterfaceMapper.updateOrgByOrgId(
                    orgId, name, short_name, level_num, p_id, order_num, memo,
                    type, org_code, ext1, ext2, org_type, creator, creat_date,
                    last_update_by, username);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", orgId);
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "修改组织成功");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        } catch (Exception e) {
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", "null");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "500");
            schema.put("message", "修改组织失败");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }
    }

    //删除组织(逻辑)
    public String orgDeleteService(String bodyparam) {
        System.out.println("orgDeleteService--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);

        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        String orgId = (String) reqmap.get("bimOrgId");
        try {
            javaInterfaceMapper.deleteOrgByOrgId(orgId);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", orgId);
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "删除组织成功");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        } catch (Exception e) {
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", "null");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "500");
            schema.put("message", "删除组织失败");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }

    }

    public String orgActiveService(String bodyparam) {
        System.out.println("orgActiveService--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);

        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        String orgId = (String) reqmap.get("bimOrgId");
        try {
            javaInterfaceMapper.activeOrgByOrgId(orgId);
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", orgId);
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "启用组织成功");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        } catch (Exception e) {
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("uid", "null");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "500");
            schema.put("message", "启用组织失败");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }
    }

    //批量查询组织
    public String selectOrgList(String bodyparam) {
        System.out.println("selectOrgList--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);

        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        List<OrgEntity> orgEntityList = javaInterfaceMapper.selectOrgList();
        List<String> orgIdList = new ArrayList<>();
        for (int i = 0; i < orgEntityList.size(); i++) {
            orgIdList.add(orgEntityList.get(i).getOrg_id() + "");
        }
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("resultCode", "0");
        schema.put("message", "QueryAllOrgIdsService success...");
        schema.put("orgIdList", orgIdList);
        schema.put("bimRequestId", reqmap.get("bimRequestId"));
        String mapJson = JSON.toJSONString(schema);
        System.out.println("response----->" + mapJson);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;

    }

    //根据组织id查询组织信息
    public String selectOrgByOrgId(String bodyparam) {
        System.out.println("selectOrgList--param-->" + bodyparam);
        bodyparam = bamboocloudUtils.getPlaintext(bodyparam, "123456", "AES");
        System.out.println("json--param-->" + bodyparam);
        Map<String, Object> reqmap = (Map<String, Object>) JSON.parse(bodyparam);

        String username = (String) reqmap.get("bimRemoteUser");
        String password = (String) reqmap.get("bimRemotePwd");

        bamboocloudUtils.checkUsernamePassword(username, password);

        String orgId = (String) reqmap.get("bimOrgId");
        try {
            OrgEntity orgEntity = javaInterfaceMapper.selectOrgByOrgId(orgId);
            Map<String, Object> schema = new HashMap<String, Object>();
            //组织信息集合
            Map<String, Object> orgInfo = new HashMap<>();
            orgInfo.put("name", orgEntity.getName());
            orgInfo.put("short_name", orgEntity.getShort_name());
            orgInfo.put("level_num", orgEntity.getLevel_num());
            orgInfo.put("p_id", orgEntity.getP_id());
            orgInfo.put("order_num", orgEntity.getOrder_num());
            orgInfo.put("memo", orgEntity.getMemo());
            orgInfo.put("type", orgEntity.getType());
            orgInfo.put("org_code", orgEntity.getOrg_code());
            orgInfo.put("ext1", orgEntity.getExt1());
            orgInfo.put("ext2", orgEntity.getExt2());
            orgInfo.put("org_type", orgEntity.getOrg_type());
            orgInfo.put("creator", orgEntity.getCreator());
            orgInfo.put("creat_date", orgEntity.getCreat_date());
            orgInfo.put("last_update_by", orgEntity.getLast_update_by());
            orgInfo.put("last_update_date", orgEntity.getLast_update_date());

            schema.put("organization", orgInfo);
            schema.put("resultCode", "0");
            schema.put("message", "success...");
            schema.put("bimRequestId", reqmap.get("bimRequestId"));

            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        } catch (Exception e) {
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("bimRequestId", reqmap.get("bimRequestId"));
            schema.put("resultCode", "0");
            schema.put("message", "error...");
            String mapJson = JSON.toJSONString(schema);
            System.out.println("response----->" + mapJson);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }

    }


}
